package com.example.demo.config;

import com.example.demo.handler.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMVCConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry){
        // 配置跨域
        registry.addMapping("/**").allowedOrigins("http://localhost:8888");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 配置将要拦截的接口
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/register")
                .excludePathPatterns("/currentUser")
                .excludePathPatterns("/listProblem/{page}/{pageSize}/{token}")
                .excludePathPatterns("/search");
    }
}
